package com.ebelter.bodyfatscale.moudules.https;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 简单的json字符长统一放在这里生成
 * 没必要单独写一个bean对象然后再gson.toJosn
 */

public class BaseJsonGenerator {

    //获取json数据{"email":"1070@qq.com","type":1}
    public String getEmailValidateRJ(String email,int type,String language){
        Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put("email",email);
        paramMap.put("type",type);
        paramMap.put("local",language);
        paramMap.put("funId",1);//可有可无的参数 1代表以色列 3代表西班牙
        return getMapRJ(paramMap);
    }


    //获取注册的json数据
    public String getRegisterRJ(String nickName,String email,String password,String birthday,
                                       int height,int weight, int sex,int profession,String code,int type){
        return "{\"nickName\":\""+nickName+"\",\"email\":\""+email+"\",\"password\":\""+
                password+"\",\"birthday\":\""+birthday+"\",\"height\":"+height+",\"weight\":"+
                weight+",\"sex\":"+sex+",\"profession\":"+profession+ ",\"code\":\""+code+"\",\"type\":"+type+"}";
    }


    //获取登录的json
    public String getLoginRJ(boolean isLoginEmail,String loginStr,String password){

        StringBuilder sb=new StringBuilder();
        sb.append("{");
        if (!isLoginEmail){
            sb.append("\"nickName\":");
        }else {
            sb.append("\"email\":");
        }

        sb.append("\"").append(loginStr).append("\",");

        sb.append("\"password\":\"").append(password).append("\"}");
        return sb.toString();

    }

    private boolean isEmpty(String str){
        return str==null||str.length()==0;
    }



    //邮箱或者昵称验证
    public String getEmailorNicknameValidateRJ(String validateStr, boolean isValidateEmail) {
        StringBuilder sb=new StringBuilder();
        sb.append("{");
        if (isValidateEmail){
            sb.append("\"email\":");
        }else {
            sb.append("\"nickName\":");
        }
        sb.append("\"").append(validateStr).append("\"}");
        return sb.toString();
    }

    //拼凑所有的键值对json信息
    public String getMapRJ(Map<String, Serializable> paramMap) {

        StringBuilder sb=new StringBuilder();
        sb.append("{");

        for(String key : paramMap.keySet()){
            sb.append("\"").append(key).append("\":");
            Serializable value = paramMap.get(key);
            if (value instanceof String){
                sb.append("\"").append((String) value).append("\"");
            }else if (value instanceof Integer){
                sb.append(value);
            }else if (value instanceof Float){
                sb.append(value);
            }else if (value instanceof Long){
                sb.append(value);
            }
            sb.append(",");
        }
        sb.deleteCharAt(sb.lastIndexOf(","));
        sb.append("}");

        return sb.toString();
    }
}
